﻿package com.young.android.business;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.young.android.atomoperation.OperItem;
import com.young.android.common.Const;
import com.young.android.common.PubFunction;
import com.young.android.communication.NetResource;
import com.young.android.entity.Item;

public class XMLPullHandler {

    ArrayList<Item> feed = null;
    Item rssItem = null;
    String belong; // 记录条目来源

    XmlPullParserFactory factory;
    XmlPullParser xpp;

    public XMLPullHandler(String url) throws XmlPullParserException, IOException {
        belong = url;
        factory = XmlPullParserFactory.newInstance();
        xpp = factory.newPullParser();
        xpp.setInput(NetResource.getNetStream(url), null);

    }

    public ArrayList<Item> getFeed() {
        return feed;
    }

    public void processDocument() throws XmlPullParserException, IOException {
        int eventType = xpp.getEventType();
        boolean flag = true;
        do {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                feed = new ArrayList<Item>();
            } else if (eventType == XmlPullParser.START_TAG) {
                flag = processStartTag(xpp);
            } else if (eventType == XmlPullParser.END_TAG) {
                processEndTag(xpp);
            }
            // 如果process正确，说明数据库中无此条目，继续，若为false，则存在条目，中断
            if (flag) {
                eventType = xpp.next();
            } else {
                eventType = XmlPullParser.END_DOCUMENT;
            }
        } while (eventType != XmlPullParser.END_DOCUMENT);
    }

    public boolean processStartTag(XmlPullParser xpp) throws XmlPullParserException, IOException {
        String name1 = xpp.getName();
        if (name1.equals("item")) {
            rssItem = new Item();
            return true;
        }

        if (rssItem != null) {
            if (name1.equals("title")) {
                rssItem.title=PubFunction.parseTitle(xpp.nextText());
                rssItem.belong=belong;
                if (OperItem.isExist(rssItem)) { // 查询数据库中是否存在
                    return false;
                }
            } else if (name1.equals("link")) {
                rssItem.link=xpp.nextText();
            } else if (name1.equals("description")) {
                rssItem.description=PubFunction.parseHtml(xpp.nextText());
            } else if (name1.equals("category")) {
                rssItem.category=xpp.nextText();
            } else if (name1.equals("pubDate")) {
                rssItem.date=PubFunction.parseDate(xpp.nextText());
            }
            // else if (name1.equals("content:encoded")) {
            // rssItem.setContent(xpp.nextText());
            // }
        }
        return true;
    }

    public void processEndTag(XmlPullParser xpp) {
        if (xpp.getName().equals("item")) {
            rssItem.state=Const.ITEM_UNREAD;
            OperItem.insertItem(rssItem);
            feed.add(rssItem);
            rssItem = null;
        }
    }

    

}
